package com.wms.ymcc;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.factory.Factory.Payment;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.facetoface.models.AlipayTradePrecreateResponse;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.junit.Test;

/**
 * @author : 阿盛哟
 * @description :
 * @createDate : 2025/6/23 9:18
 */
public class PayTest {

    @Test
    public void test1(){
        // 1. 设置参数（全局只需设置一次）
        Factory.setOptions(getOptions());
        try {
            // 2. 发起API调用（以创建当面付收款二维码为例）
//            AlipayTradePrecreateResponse response = Payment.FaceToFace()
//                    .preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
//pay(String subject , String outTradeNo, String totalAmount, String returnUrl)
            // returnUrl：  页面成功的地址
            AlipayTradePagePayResponse alipayTradePagePayResponse = Payment.Page().pay(
                    "测试使用",
                    "2234567811",
                    "5799.00",
                    "http://www.baidu.com");

            System.out.println(alipayTradePagePayResponse);
            // 3. 处理响应或异常
            if (ResponseChecker.success(alipayTradePagePayResponse)) {
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + alipayTradePagePayResponse.getBody() + "，" + alipayTradePagePayResponse.body);
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    /**
     * 获取获取与支付宝交互的配置参数
     * @return
     */
    private static Config getOptions() {
        Config config = new Config();
        config.protocol = "https";
        config.gatewayHost = "openapi-sandbox.dl.alipaydev.com";
        config.signType = "RSA2";
        config.appId = "9021000149684098";
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCWHjYQ7JhtnOcTgSydnKZmGuA+2Ku0hWFBQkLBLG9hLCWt3iw8oWdCMzpF7BMMq25ltPLVAHsXUjAYKyH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uqN8vqtV0rBdpZy0A8wqGor9kFRaiVU/j38bmp7uZLmmQsi0teCuy6vnO5V5EFp388IiOYQKBgGtnnJAl1LzKfpi1ZPEWJ1wuGp2PL1DhioADQlPBZWHKHoedmw5xPdmk44unXlZ/KnYgU1VF/O6j7ZhyEe3R98mCopMD7FUsdBX+UMhLmrfb0rf7VXZFvbk+fCQ0D5vGeavUwzd4413oyzBSZ48HMJHT54Ixhq/fPv0mD3epiOgNAoGBAKdZW3M7by57FBOLiTarblThmw9qLDRKtolXtpa6lyfp4DQ0B/gJsGN3owKDpLGgnNtsQWhBvleKhA1n7z3P0GnlscEouKdDd9R8HLt90m3rqxl+bnGaN6T3v63xzH7Kxy5StR5DM1vfYigpWoUb2dKEUKN0ZCkv/ADryXYbXI+B";
        //注：证书文件路径支持设置为文件系统中的路径或CLASS_PATH中的路径，优先从文件系统中加载，加载失败后会继续尝试从CLASS_PATH中加载
//        config.merchantCertPath = "<-- 请填写您的应用公钥证书文件路径，例如：/foo/appCertPublicKey_2019051064521003.crt -->";
//        config.alipayCertPath = "<-- 请填写您的支付宝公钥证书文件路径，例如：/foo/alipayCertPublicKey_RSA2.crt -->";
//        config.alipayRootCertPath = "<-- 请填写您的支付宝根证书文件路径，例如：/foo/alipayRootCert.crt -->";
        //注：如果采用非证书模式，则无需赋值上面的三个证书路径，改为赋值如下的支付宝公钥字符串即可
        config.alipayPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoD+hhpDkIiMPd2NS8Ds1VPrH10Dh/TCHKEZarUcL5/VCd1735nh5AMXQmjTET1/fZgQ0rmaTb8eciTbqGTDurDcJxvp5b6BIO2ABrRAGCZ6HkjdjVl+jGGfCoJ7UXdi7KxoWq4vhAAfV6Y3WhScj4x24instwPAvUdqjRByVtbbRqCaMlPxPIcmIpOgJALO+tLBX7YmAmVB1URoP0dVEptAR3xqrZZoMDZ5VdCWhM0ewLmTPuzCw+C+8cvV8jGP/8zXWNFMTGzcs49NI3FGpzkxaNb6lHZKC2YXC55d09EeDdTG7fMNT3HmO9y+sgQESjmTfNC2DcaCQwi6GO3X19QIDAQAB";
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = "http://localhost:10010/pay/notifyUrl";
        return config;
    }
}